#include "BRNNRecordLayer.h"

bool BRNNRecordLayer::init()
{
	if (!Layer::init())
		return false;

// 	LayerColor* layerColor = LayerColor::create(Color4B(0, 0, 0, 180));
// 	addChild(layerColor);
// 	layerColor->setContentSize(Director::getInstance()->getVisibleSize());

	layoutMain = CSLoader::createNode("recordLayer.csb");
	addChild(layoutMain);

	auto btn = (Button*)layoutMain->getChildByName("Button_close");
	btn->addTouchEventListener(CC_CALLBACK_2(BRNNRecordLayer::BtnCallBack, this));
	return true;
}

void BRNNRecordLayer::onEnter()
{
	Layer::onEnter();
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(BRNNRecordLayer::onTouchBegan, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	_touchListener = listener;
}

void BRNNRecordLayer::onExit()
{
	_eventDispatcher->removeEventListener(_touchListener);
	_touchListener = nullptr;
	Layer::onExit();
}

bool BRNNRecordLayer::onTouchBegan( Touch *pTouch, Event *pEvent )
{
// 	if (!layoutMain->getChildByName("Panel_chat")->getBoundingBox().containsPoint(pTouch->getLocation()))
// 	{
// 		this->runAction(RemoveSelf::create());
// 	}
	return true;
}

void BRNNRecordLayer::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
		case cocos2d::ui::Widget::TouchEventType::ENDED:
		{
			if (btn->getName().compare("Button_close") == 0)
			{
				this->runAction(RemoveSelf::create());
			}
		}
	}
}

void BRNNRecordLayer::showList(std::vector<tagServerGameRecord> recordList)
{
	ListView* listView = (ListView*)layoutMain->getChildByName("ListView_record");
	listView->removeAllItems();

	Layout* panelItem = (Layout*)layoutMain->getChildByName("Panel_item");
	for (int i = 0; i < recordList.size(); ++i)
	{
		Layout* newItem = (Layout*)panelItem->clone();
		listView->pushBackCustomItem(newItem);

		tagServerGameRecord pNet = recordList[i];
		ImageView* image1 = (ImageView*)newItem->getChildByName("Image_child1");
		image1->loadTexture(pNet.bWinShunMen ? "worldWLList/07.png" : "worldWLList/06.png");

		ImageView* image2 = (ImageView*)newItem->getChildByName("Image_child2");
		image2->loadTexture(pNet.bWinDuiMen ? "worldWLList/07.png" : "worldWLList/06.png");

		ImageView* image3 = (ImageView*)newItem->getChildByName("Image_child3");
		image3->loadTexture(pNet.bWinDaoMen ? "worldWLList/07.png" : "worldWLList/06.png");

		ImageView* image4 = (ImageView*)newItem->getChildByName("Image_child4");
		image4->loadTexture(pNet.bWinHuang ? "worldWLList/07.png" : "worldWLList/06.png");
	}
}
